#include "comsniffer.h"
#include <qextserialport.h>
#include <QThread>
#include <QTextStream>
#include <QtDebug>
#include <QRegExp>


void	ComSniffer::run()
{
	QString str,buffer;
	char charset;
	bool ok;
	int ves = 0,bytesavailable;
//	qint64 len;
//	QTextCodec *codec = QTextCodec::codecForName("UTF-8");
//	port = new QextSerialPort("COM1");
	port = new QextSerialPort("/dev/ttyS0");
//	port = new QextSerialPort("/dev/ttyUSB0");
	port->setBaudRate(BAUD2400);
	port->setFlowControl(FLOW_OFF);
	port->setParity(PAR_NONE);
	port->setDataBits(DATA_8);
	port->setStopBits(STOP_1);
	if(port->open(QIODevice::ReadWrite))
	{
		qDebug() << "port open";
	}
	//QTextStream comStream(port);
	//comStream << "hi\n";
	while(true)
	{
//		str = comStream.readLine();
//		comStream.bytesAvailable();
		str = "";
		while(true)
		{
			bytesavailable = port->bytesAvailable();
			if (bytesavailable > 0)
			{
				qDebug() << bytesavailable << "<---gap--->";
				for (int i = 0; i < bytesavailable; i++)
				{
					port->getChar(&charset);
					buffer[i] = charset;
				}
//			if (charset==0x02)
//			if (charset=='m')
			}
		}
		
//		qDebug() << str;
//		str.remove(0,1);
//		str.remove(7,3);
//		str.remove(QChar(' '));
		
//		qDebug() << str;
		str.toInt(&ok);
		if(str!=QString("") & ok)
		{
		//	qDebug() << ok;
		//	qDebug() << str;
			if (ves!=str.toInt())
			{
				ves=str.toInt();
				qDebug() << ok;
				qDebug() << ves;
				emit comSignal(ves);
			}
		}
	//	sleep(10);
	}
}
